import { projects } from "../db";
import { ObjectId, Filter, Timestamp } from "mongodb";
import dayjs from "dayjs";

import * as db from "../db";
import { stats } from "../libs/stats";
import { IProject } from "../models/project";

/**
 * 查询项目列表
 * @param account: 用户账号
 * @returns
 */
export async function list(account: string) {
  const items = await db.projects.find({ account: account }).toArray();
  const total = items.length;

  return {
    items,
    total,
  };
}

/**
 * 创建项目
 * @param record
 * @returns
 */
export async function create(record: IProject) {
  // 根据session查找用户(使用projection返回部分信息)
  const user = await db.users.findOne({
    _id: record.personId,
  });
  if (!user) {
    throw stats.ErrUserNotFound;
  }
  await db.projects.insertOne(record);
}

/**
 * 删除项目
 * @param _id 项目id
 * @param uid: 当前用户id
 */
export async function remove(_id: string, uid: string) {}

/**
 * 搜索项目
 * @param keywords 搜索关键词
 */
export async function search(keywords: string) {}
